<template>
  <a-row class="globalHeader" align="center" :wrap="false">
    <a-col flex="auto">
      <a-menu
        mode="horizontal"
        :selected-keys="selectedKeys"
        @menu-item-click="doMenuClick"
      >
        <a-menu-item
          key="0"
          :style="{ padding: 0, marginRight: '38px' }"
          disabled
        >
          <div class="title-bar" @click="goToHome">
            <img class="logo" src="../assets/suse-icon.jpg" />
            <div class="title">SUSE-OJ</div>
          </div>
        </a-menu-item>
        <a-menu-item v-for="item in visibleRoutes" :key="item.path">
          {{ item.name }}
        </a-menu-item>
      </a-menu>
    </a-col>
    <a-col flex="200px">
      <div v-if="store.state.user.loginUser.userRole == 'notLogin'">
        <a-button type="primary" @click="goToLogin">登录</a-button>
      </div>
      <div v-else class="flex items-center">
        <a-dropdown>
          <a-button type="text">{{
            store.state.user?.loginUser?.userName
          }}</a-button>
          <template #content>
            <!--            <a-doption @click="$router.push('/profile')">个人主页</a-doption>-->
            <a-doption @click="logout">退出登录</a-doption>
          </template>
        </a-dropdown>
      </div>
    </a-col>
  </a-row>
</template>

<script setup lang="ts">
import { routes } from "@/router/routes";
import { useRouter, useRoute } from "vue-router";
import { computed, ref } from "vue";
import { useStore } from "vuex";
import checkAccess from "@/access/checkAccess";
import ACCESS_ENUM from "@/access/accessEnum";
import { UserControllerService } from "../../generated";
import message from "@arco-design/web-vue/es/message";

const store = useStore();
const router = useRouter();
const selectedKeys = ref(["/"]);

const visibleRoutes = computed(() => {
  return routes.filter((item, idx) => {
    if (item.meta?.hideInMenu) {
      return false;
    }
    return checkAccess(
      store.state?.user?.loginUser,
      item.meta?.access as string
    );
  });
});

router.afterEach((to, from, failure) => {
  selectedKeys.value = [to.path];
});

setTimeout(() => {
  store.dispatch("user/getLoginUser", {
    userName: "QB管理员",
    userRole: ACCESS_ENUM.ADMIN,
  });
  console.log(store.state?.user?.loginUser);
}, 2000);

const doMenuClick = (key: string) => {
  router.push({
    path: key,
  });
};
// Add this function to handle login button click
const goToLogin = () => {
  router.push({
    path: "/user/login",
  });
};

const goToHome = () => {
  router.push({
    path: "/",
  });
};

const logout = async () => {
  const res = await UserControllerService.userLogoutUsingPost();
  // 退出登录，刷新页面
  if (res.code === 0) {
    location.reload();
  } else {
    message.error("注册失败，" + res.message);
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.flex {
  display: flex;
  align-items: center;
}

.items-center {
  align-items: center;
}

.ml-4 {
  margin-left: 1.6rem; /* 根据需要调整间距 */
}
.title-bar {
  display: flex;
  align-items: center;
}

.logo {
  width: 100px;
  height: 100px;
}

.title {
  color: #42b983;
  font-size: 25px;
  margin-left: 10px;
}
</style>
